package com.yuyuto.conn.subscriber;


import android.content.Context;
import android.widget.Toast;

import com.yuyuto.conn.exception.ApiException;
import com.yuyuto.conn.util.DialogHelper;

/**
 * Created by Demon on 2016/11/28.
 */

public class RxSubscriber<T> extends ErrorSubscriber<T> {

    protected Context mContext;
    private boolean isShowDialog;
    private boolean mIsShowResponseErr;

    public RxSubscriber(Context context) {
        this(context, true);
    }

    public RxSubscriber(Context context, boolean isShowDialog) {
        //默认不显示业务响应错误
        this(context, isShowDialog, false);
    }

    public RxSubscriber(Context context, boolean isShowDialog, boolean isShowResponseErr) {
        this.mContext = context;
        this.isShowDialog = isShowDialog;
        mIsShowResponseErr = isShowResponseErr;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (isShowDialog) {
            DialogHelper.showProgressDlg(mContext, "正在加载数据");
        }
    }

    @Override
    public void onCompleted() {
        DialogHelper.stopProgressDlg();
    }

    @Override
    protected void onError(ApiException ex) {
        DialogHelper.stopProgressDlg();
        if (mIsShowResponseErr) {
            Toast.makeText(mContext, ex.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onNext(T t) {

    }

    public void unsubscribeCus() {
        DialogHelper.stopProgressDlg();
        if (!isUnsubscribed()) {
            unsubscribe();
        }
    }

}
